var vehicleMonitor = {
    init : function(){
    	this.base64 = new Base64();
       mapTool.init();// 地图初始化
       this.trafficLayer=null;
       this.satelliteLayer=null;
       this.roadNetLayer =null;
       this.vehicleTimer=null;       // 订阅定时器
       this.terminalMap = new HashMap();
       this.vehicleMap = new HashMap();
       this.initVehicleList(); 
       this.myChart=null;
       this.time=[];
       this.speed=[];
       this.initEcharts();
       this.subScribe();
       this.bind();
       this.lineArr=[];
       this.currentPoint=[];
       this.lastPoint=[];
       this.imageChange();
    },
    initVehicleList : function(){
    	var isMulti=global.getUrlParam("isMulti");
    	if(isMulti==1){
			$("#tooldiv").css("display","block");
		}	
    	var vehicle_no_color = this.base64.decode(global.getUrlParam("vehicle_no_color"));
    	vehicleCar = vehicle_no_color.split('_');
    	document.title = vehicleCar[0]+ '_' + global.carColor(vehicleCar[1]) + '-重点监控';
    	$("#selectVehicleSel").select2({
	        theme: "default",
	        allowClear: true,
	        placeholder: "请选择车辆",
	        ajax:{
	            url:global.url.domain + "gps-web/gis/playback/loadUserVehicle.html",
	            dataType:"json",
	            delay:250,
	            data:function(params){
	                return {
	                    name: params.term,
	                    page: params.page || 1
	                };
	            },
	            cache:true,
	            processResults: function (res, params) {
			    	if(res){
			    		var options = [];
	    				$.each(res,function(index,obj) {
	    					var vehicle_no_color = obj.vehicle_no + "_" + obj.vehicle_color;
	    					var option = {"id":vehicle_no_color, "text":obj.vehicle_no + "(" + obj.vehicle_color_name + ")"};
    	                    options.push(option);
	    				});
    	                return {
    	                	results: options,
    	                    pagination: {
    	                        more:false
    	                    }
    	                };
			    	}
	            },
	            escapeMarkup: function (markup) { return markup; },
	            minimumInputLength: 1
	        }
	    });
//    	$.ajax({
//    		type : 'post',
//    		url : global.url.domain+"gps-web/gis/monitor/queryVehicleList.html",
//    		cache : false,
//    		dataType : "JSON",
//    		success : function(data) {
//    			
//    			if(data){
//    				$('#selectVehicleSel').select2({
//    				  data: data
//    				});			
//    				if(vehicle_no_color){
//    					$('#selectVehicleSel').val(vehicle_no_color).trigger('change');
//    				}else{
//    					$('#selectVehicleSel').val(0).trigger("change");
//    				}
//    				
//    			}
//    			
//    		}
//    	});
    },
    subScribe : function(no_color){
    	if(no_color){
    		var vehicle_no_color = no_color;
    	}else{
    		 var vehicle_no_color = this.base64.decode(global.getUrlParam("vehicle_no_color"));
    	}
    //	 var vehicle_no_color = global.base64.decode(global.getUrlParam("vehicle_no_color"));
//    	 if(commno){
//    		 CommNO=commno;
//    	 }
//    	 if(CommNO){
//    		 console.log();
//    	 }else{
    		 //global.ajaxLoading.pop("当前没有定位数据",true,2000);
//    		 return;
//    	 }
	   	 var getSubScribeData = function(vehicle_no_color){
	   		var getTimestamp=new Date().getTime();
	   		 $.ajax({
	                url : global.url.domain+'gps-web/gis/monitor/subScribe.html?v='+getTimestamp,
	                data :{"vehicle_no_color":encodeURI(vehicle_no_color)},
	                success : function(data){
	                    if(data){
	                    	var result = JSON.parse(data);
	                    	if(result.data.length>0){
	                    		
	                    		mapTool.clearMarker(result.data[0].vehicleId);
//	                    		result[0].vehicleNo=vehicleMonitor.vehicleMap.get(result[0].vehicleId);
		                    	mapTool.addMarker(result.data[0]);
		                    	mapTool.setCenter(result.data[0]);
		                    	vehicleMonitor.drawTrack(result.data[0]);    	
		                    	vehicleMonitor.time.push(result.data[0].gpsTimeS);
		                    	vehicleMonitor.speed.push(result.data[0].gpsSpeedF);
		                        vehicleMonitor.myChart.setOption({
		                            xAxis: {
		                                data: vehicleMonitor.time
		                            },
		                            series: [{
		                                name:'速度',
		                                data: vehicleMonitor.speed
		                            }]
		                        });
	                    	}else{
		                    	global.ajaxLoading.pop("当前车辆无定位数据",true,2000);
		                    }
	                    }
	                }
	            });
	   	 };
	   	 getSubScribeData(vehicle_no_color);
	   	 clearInterval(vehicleMonitor.vehicleTimer);
	   	 if(vehicle_no_color){
	   		 vehicleMonitor.vehicleTimer = setInterval(function(){
	 	   		getSubScribeData(vehicle_no_color);          
	 		 },10000);
	   	 }else{
	   		//global.ajaxLoading.pop("当前车辆无定位数据",true,2000);
	   	 }
	   		
    },
    /**
     * 画轨迹
     * @param data
     */
    drawTrack : function(data){
    	vehicleMonitor.currentPoint=[data.longitude,data.latitude];
    	if(vehicleMonitor.currentPoint.length==2&&vehicleMonitor.lastPoint.length==2){
    		vehicleMonitor.lineArr.push(vehicleMonitor.currentPoint);
        	vehicleMonitor.lineArr.push(vehicleMonitor.lastPoint);
    	}
    	if(vehicleMonitor.lineArr.length==2){
    		mapTool.drawTrack(data,vehicleMonitor.lineArr);
    		vehicleMonitor.lineArr=[];
    	}
    	vehicleMonitor.lastPoint=vehicleMonitor.currentPoint;
    	vehicleMonitor.currentPoint=[];
    },
    initEcharts : function(){
    	vehicleMonitor.myChart = echarts.init(document.getElementById('speedCharts'));
    	var option = {
    		    xAxis: {
    		    	type : 'category',
    	            boundaryGap : false,
    		        data:vehicleMonitor.time 
    		    },
    		    grid:{
    		    	x:30,
    		    	y:30,
    		    	x2:5,
    		    	y2:5
    		    	
    		    },
    		    calculable : true,
    		    tooltip: {
    		        trigger: 'axis',
    		        formatter: function (params) {
    		            params = params[0];
    		            return "时间："+params.name+"<br/>"+"速度："+params.value;
    		        },
    		        axisPointer: {
    		            animation: false
    		        }
    		    },
    		    yAxis: {
    		        name : '速度(km/h)',
    			    type : 'value',
    			    max : 150,
    			    data : [30,60,90,120,150]
    		    },
    		    series: [
    		        {
    		            name:'速度',
    		            type:'line',
    		            smooth:true,
    		            stack: 'a',
    		            data: [0]
    		        }
    		    ]
    		};
    	vehicleMonitor.myChart.setOption(option);
    },
    bind : function(){
    	 $("#mTrafficLi").toggle(function(){
    		 vehicleMonitor.trafficLayer = new AMap.TileLayer.Traffic({zIndex:10}); //实时路况图层
    		 vehicleMonitor.trafficLayer.setMap(mapTool.map);
			 $('.t8').addClass("active");
		 },function(){
			 vehicleMonitor.trafficLayer.setMap(null);
			 $('.t8').removeClass("active");
		 });
		 
		 $("#distanceLi").click(function(){
			  mapTool.map.plugin(["AMap.RangingTool"], function() {
			     ruler = new AMap.RangingTool(mapTool.map);
		         AMap.event.addListener(ruler, "end", function(e) {
		            ruler.turnOff();
		            $('.t6').removeClass("active");
		         });
		         ruler.turnOn();
		         $('.t6').addClass("active");
			  });
		 });		
	
		 $("#mSatellite").toggle(function(){
			 $('#mRoadlite').removeClass('dn');
			 $('#mRoadlite').addClass('selected');
			 vehicleMonitor.satelliteLayer = new AMap.TileLayer.Satellite({zIndex:10}); //实时路况图层
    		 vehicleMonitor.satelliteLayer.setMap(mapTool.map);
			 vehicleMonitor.roadNetLayer = new AMap.TileLayer.RoadNet({zIndex:10}); //实例化路网图层
			 vehicleMonitor.roadNetLayer.setMap(mapTool.map);    		 
			 $('.t10').addClass("active");
		 },function(){
			 vehicleMonitor.satelliteLayer.setMap(null);
			 vehicleMonitor.roadNetLayer.setMap(null);
			 $('.t10').removeClass("active");
			 $('#mRoadlite').addClass('dn');
			 $('#mRoadlite').removeClass('selected');
		 });
		$('#mRoadlite').click(function(){
			if($('#mRoadlite').hasClass('selected')){
				 vehicleMonitor.roadNetLayer.setMap(null); 
				 $('#mRoadlite').removeClass('selected');
			}else{				
				 vehicleMonitor.roadNetLayer = new AMap.TileLayer.RoadNet({zIndex:10}); //实例化路网图层
				 vehicleMonitor.roadNetLayer.setMap(mapTool.map); 
				 $('#mRoadlite').addClass('selected');
			}
		});
    	$("#selectVehicleSel").on("change", function(e) { 
    		vehicleMonitor.currentPoint=[];
    		vehicleMonitor.lastPoint=[];
    		clearInterval(vehicleMonitor.vehicleTimer);  		
    		mapTool.clearAllMarker();
    		mapTool.clearAllPolyline();
    		var vehicle_no_color = $("#selectVehicleSel").val();
    		vehicleMonitor.subScribe(vehicle_no_color);
    		
    		vehicleMonitor.time=[];
        	vehicleMonitor.speed=[];
    		vehicleMonitor.initEcharts();
    		vehicleMonitor.myChart.setOption({
                xAxis: {
                    data: []
                },
                series: [{
                    name:'速度',
                    data: [0]
                }]
            });
    	});
    },
    imageChange:function(){
    	var flag = true;
    	$("#toolTopDiv #mRoadlite").click(function(){
    		if(flag){
    			$(this).removeClass('spanSelect');
    			$(this).find('.image').css('background-position','0 -434px');
    			flag=false;
    		}else{
    			$(this).addClass('spanSelect');
    			$(this).find('.image').css('background-position','0 -483px');
    			flag=true;
    		}
    	})
    }
};

vehicleMonitor.init();
